package com.gopay.checkorder.service.sentinel.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.gopay.checkorder.dao.BisTimingFuseDao;
import com.gopay.common.domain.bank.bcdl.BisTimingFuse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gopay.checkorder.dao.BankReturnMsgForFusedDAO;
import com.gopay.checkorder.service.sentinel.BcdlOrderSentinel;
import com.gopay.common.domain.bank.bcdl.BisBankReturnMsgForFused;
import com.gopay.common.domain.sentinel.bcdl.BcdlSentinelResult;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.sentinel.BcdlOrderSentinelRemoteService;

@Service("bcdlOrderSentinelFacade")
public class BcdlOrderSentinelFacade implements BcdlOrderSentinelRemoteService {
	
	private final static Logger logger = LoggerFactory.getLogger(BcdlOrderSentinelFacade.class);
	
	@Autowired
	private Map<String,BcdlOrderSentinel> allBank;

	@Resource(name = "bankReturnMsgForFusedDAO")
	private BankReturnMsgForFusedDAO bankReturnMsgForFusedDAO;

	@Resource(name = "bisTimingFuseDao")
	private BisTimingFuseDao bisTimingFuseDao;

	public static Map<String, BisBankReturnMsgForFused> codeMap = new HashMap<>();

	@Override
	public boolean restart(String Id) {
		// TODO Auto-generated method stub
		BcdlOrderSentinel bcdlOrderSentinel = allBank.get(Id);
		//-------modify----2018/2/8----boai------新增可强制解除定时熔断----
		if(bcdlOrderSentinel!=null){
			BisTimingFuse exeTimingFuse = bisTimingFuseDao.findBisTimingFuseByExe(Id);
			if(exeTimingFuse==null){//判断是否有执行中定时熔断
				bcdlOrderSentinel.restart();
			}else{
				bcdlOrderSentinel.restartForTime(exeTimingFuse.getSq());
			}
		}
		logger.error("[fuseWiring]" + Id + ", Sentinel restart time = " + DateUtils.format(new Date()));
		return true;
	}

	@Override
	public boolean stop(String Id) {
		// TODO Auto-generated method stub
		BcdlOrderSentinel bcdlOrderSentinel = allBank.get(Id);
		if(bcdlOrderSentinel!=null)
			bcdlOrderSentinel.stop(0);
		logger.error("[fuseWiring]" + Id + ", Artificial Sentinel time = " + DateUtils.format(new Date()));
		return true;
	}

	@Override
	public List<BcdlSentinelResult> getBankSentinelResult() {
		// TODO Auto-generated method stub
		List<BcdlSentinelResult> result = new ArrayList<BcdlSentinelResult>();
		for (Map.Entry<String,BcdlOrderSentinel> entry : allBank.entrySet()) {
			BcdlOrderSentinel bcdlOrderSentinel = entry.getValue();
			BcdlSentinelResult sentinelResult = new BcdlSentinelResult();
			sentinelResult.setBankCode(bcdlOrderSentinel.getBank());
			sentinelResult.setIsFuse(bcdlOrderSentinel.isFuse());
			sentinelResult.setId(entry.getKey());
			result.add(sentinelResult);
		}
		Collections.sort(result, new Comparator<BcdlSentinelResult>() {
			public int compare(BcdlSentinelResult o1, BcdlSentinelResult o2) {
				return o1.getId().compareTo(o2.getId());
			};
		});
		return result;
	
	}

	/**
	 * 刷新熔断错误码缓存
	 * @return
	 */
	@Override
	@PostConstruct
	public boolean reInitSet() {
		try {
			List<BisBankReturnMsgForFused> list = bankReturnMsgForFusedDAO.getBankReturnMsg();
			codeMap.clear();
			for (BisBankReturnMsgForFused bisBankReturnMsgForFused : list) {
				codeMap.put(bisBankReturnMsgForFused.getReturnCode()+"-"+bisBankReturnMsgForFused.getStlmEx(), bisBankReturnMsgForFused);
            }
            return true;
		} catch (Exception e) {
			logger.error("刷新熔断错误码缓存异常",e);
			return false;
		}
	}

	public Map<String, BisBankReturnMsgForFused> getSentinelCodeMap() {
		Map<String, BisBankReturnMsgForFused> codeMap = new HashMap<>();
		try {
			List<BisBankReturnMsgForFused> list = bankReturnMsgForFusedDAO.getBankReturnMsg();
			for (BisBankReturnMsgForFused bisBankReturnMsgForFused : list) {
				codeMap.put(bisBankReturnMsgForFused.getReturnCode()+"-"+bisBankReturnMsgForFused.getStlmEx(), bisBankReturnMsgForFused);
			}
		} catch (Exception e) {
			logger.error("刷新熔断错误码缓存异常",e);
		}
		return codeMap;
	}
}
